package com.nf1000.balancelibrary;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by Jason on 2016/3/22.
 */
public class ScaneBluetoothService extends Service {
    public static String ACTION = "ScaneBluetoothService";
    private static String TAG = "ScaneBluetoothService";
    public static boolean discoverying = false;
    private BluetoothAdapter mBtAdapter;
    Set<BluetoothDevice> devices = new HashSet();
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("android.bluetooth.adapter.action.DISCOVERY_STARTED".equals(action)) {
                Intent device = new Intent("ACTION_START_DISCOVERY");
                ScaneBluetoothService.this.sendBroadcast(device);
            } else if ("android.bluetooth.device.action.FOUND".equals(action)) {
                BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.e(ScaneBluetoothService.TAG, "ACTION_FOUND!" + device.getName());
                if (device != null && device.getName() != null && "Electronic Scale".equals(device.getName())) {
                    Log.e(ScaneBluetoothService.TAG, "连接未绑定设备 ：" + device.getName());
                    ScaneBluetoothService.this.devices.add(device);
                    if (BluetoolUtil.mChatService != null) {
                        BluetoolUtil.mChatService.connect(device, true);
                        BluetoolUtil.lastDevice = device;
                        ScaneBluetoothService.this.mBtAdapter.cancelDiscovery();
                        ScaneBluetoothService.discoverying = false;
                        Log.e(ScaneBluetoothService.TAG, "扫描完成：" + ScaneBluetoothService.this.devices.size());
                        ScaneBluetoothService.this.stopSelf();
                    }
                }
            } else if ("android.bluetooth.adapter.action.DISCOVERY_FINISHED".equals(action)) {
                ScaneBluetoothService.discoverying = false;
                Log.e(ScaneBluetoothService.TAG, "扫描完成：" + ScaneBluetoothService.this.devices.size());
                ScaneBluetoothService.this.stopSelf();
            }

        }
    };

    public ScaneBluetoothService() {
    }

    public void onCreate() {
        this.mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        IntentFilter filter = new IntentFilter("android.bluetooth.device.action.FOUND");
        this.registerReceiver(this.mReceiver, filter);
        filter = new IntentFilter("android.bluetooth.adapter.action.DISCOVERY_FINISHED");
        this.registerReceiver(this.mReceiver, filter);
    }

    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.e(TAG, "开始循环扫描任务... ");
        this.devices.clear();
        this.doDiscovery();
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.mBtAdapter != null) {
            this.mBtAdapter.cancelDiscovery();
        }

        this.unregisterReceiver(this.mReceiver);
        this.stopSelf();
    }

    private void doDiscovery() {
        if (this.devices.size() > 0) {
            this.devices.clear();
        }

        if (this.mBtAdapter != null) {
            if (this.mBtAdapter.isDiscovering()) {
                this.mBtAdapter.cancelDiscovery();
                discoverying = false;
            }

            discoverying = true;
            this.mBtAdapter.startDiscovery();
        }

    }

    class ProgressBarAsyncTask extends AsyncTask<Integer, Integer, String> {
        private Set<BluetoothDevice> pairedDevices;

        public ProgressBarAsyncTask(Set<BluetoothDevice> var1) {
            this.pairedDevices = pairedDevices;
        }

        protected String doInBackground(Integer... params) {
            if (this.pairedDevices != null && this.pairedDevices.size() > 0) {
                Iterator var3 = this.pairedDevices.iterator();

                while (var3.hasNext()) {
                    BluetoothDevice bluetoothDevice = (BluetoothDevice) var3.next();
                    if (bluetoothDevice.getName().equals("Electronic Scale") && BluetoolUtil.mChatService != null && BluetoolUtil.mChatService.getState() != 3) {
                        Log.e("test", "连接........." + bluetoothDevice.getName() + "_" + bluetoothDevice.getAddress());
                        if (BluetoolUtil.mChatService.connectTest(bluetoothDevice)) {
                            try {
                                BluetoolUtil.mChatService.connect(bluetoothDevice, true);
                            } catch (Exception var5) {
                                ;
                            }
                            break;
                        }
                    }
                }
            }

            return "over";
        }

        protected void onPostExecute(String result) {
        }

        protected void onPreExecute() {
        }

        protected void onProgressUpdate(Integer... values) {
        }
    }

}
